package com.googlecode.data.objects3d
{
	import com.googlecode.data.IValueObject;
	import com.googlecode.data.materials.BitmapFileMaterialVO;
	import com.googlecode.data.materials.ColorMaterialVO;
	import com.googlecode.data.materials.FileAssetMaterialVO;
	import com.googlecode.data.materials.MaterialVO;
	import com.googlecode.data.materials.MovieMaterialVO;
	import com.googlecode.data.materials.TextMaterialVO;
	import com.googlecode.data.materials.VideoStreamMaterialVO;
	
	import flash.utils.Dictionary;
	
	public class MaterialsListObject3DVO extends Object3DVO
	{
		
		public var materialsList:Dictionary = new Dictionary();
		
		public var selectedFace:String;
		
		override public function clone(src:* = null):IValueObject
		{
			if(src == null)
			{
				src = super.clone(new MaterialsListObject3DVO());
			}else
			{
				src = super.clone(src);
			}
			
			src.selectedFace = this.selectedFace;
			
			for each(var mat:MaterialVO in this.materialsList)
			{
				(src as MaterialsListObject3DVO).materialsList[mat.face] = mat.clone();
			}
			
			return src;
		}
		
		override public function toXML(src:XML = null):XML
		{
			if(src == null)
			{
				src = super.toXML(<object3D name={this.name} label={this.label}></object3D>);
				
			}else
			{
				src = super.toXML(src);
			}
			
			src.appendChild(<materialsList></materialsList>);
			
			for each(var mat:MaterialVO in this.materialsList)
			{
				src.materialsList.appendChild(mat.toXML());
			}

			return src;
		}
		
		override public function fromXML(src:XML):void
		{
			super.fromXML(src);
			
			var materialsList:Dictionary = new Dictionary();
			var newMat:MaterialVO;
			
			for each(var mat:XML in src.materialsList.*)
			{
				if(mat.name() == ColorMaterialVO.TYPE)
				{
					newMat = new ColorMaterialVO();
					(newMat as ColorMaterialVO).fillAlpha = mat.fillAlpha;
					(newMat as ColorMaterialVO).fillColor = mat.fillColor;
					 
				}
				
				if(mat.name() == MovieMaterialVO.TYPE)
				{
					newMat = new MovieMaterialVO();
 
				}
				
				if(mat.name() == TextMaterialVO.TYPE)
				{
					newMat = new TextMaterialVO();
					(newMat as TextMaterialVO).text = mat.text;
				}
				
				if(mat.name() == BitmapFileMaterialVO.TYPE)
				{
					newMat = new BitmapFileMaterialVO();
 
				}
				
				if(mat.name() == VideoStreamMaterialVO.TYPE)
				{
					newMat = new VideoStreamMaterialVO();

					(newMat as VideoStreamMaterialVO).width = mat.width;
					(newMat as VideoStreamMaterialVO).height = mat.height;

				}
				
				if(newMat is FileAssetMaterialVO)
				{
					(newMat as FileAssetMaterialVO).filePath = mat.filePath;
					(newMat as FileAssetMaterialVO).isRelative = mat.filePath.@isRelative == "true" ? true : false;
				}
				
				newMat.face = mat.@face;
					
				materialsList[mat.@face] = newMat;
				
				this.materialsList = materialsList;
			}
		}
	}
}